﻿using System;
using System.Windows;
using nRoute.Navigation;

namespace nRoute.Behaviors
{
    public static class ShellIntegration
    {

#region Dependency Properties

        public static readonly DependencyProperty DisableOnShellStateProperty = DependencyProperty.RegisterAttached(
            "DisableOnShellState",
            typeof(bool),
            typeof(ShellIntegration),
            new PropertyMetadata(false, OnSetDisableOnShellStateCallback));

#endregion

#region Getters and Setters

        public static void SetDisableOnShellState(System.Windows.Interactivity.TriggerAction dependencyObject, bool isDefault)
        {
            dependencyObject.SetValue(DisableOnShellStateProperty, isDefault);
        }

        public static bool GetDisableOnShellState(System.Windows.Interactivity.TriggerAction dependencyObject)
        {
            if (dependencyObject == null) return false;
            return Convert.ToBoolean(dependencyObject.GetValue(DisableOnShellStateProperty));
        }

#endregion

#region Handlers

        private static void OnSetDisableOnShellStateCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            var _triggerAction = dependencyObject as System.Windows.Interactivity.TriggerAction;
            if (_triggerAction == null) return;

            if (Convert.ToBoolean(e.NewValue))
            {
                _triggerAction.IsEnabled = string.IsNullOrEmpty(Application.Current.Host.NavigationState);
            }
            else
            {
                _triggerAction.IsEnabled = true;
            }
        }

#endregion

    }
}
